import Taro from '@tarojs/taro';
import {Image, View} from '@tarojs/components';
import {connect} from "@tarojs/redux";
import {request, unite} from '../../../utils/index';
import head from "../../../asset/images/head.png";
import './arrangingCoursey.scss';

class MyCoupon extends Taro.Component {
    config = {
        navigationBarTitleText: '排课',
        navigationBarBackgroundColor: '#ffffff',
    };

    state = {
        items: [], //课程
        roomWrap: [], //常用教师列表
    };

    componentDidShow() {
        let option = this.$router.params;
        if (option.id) {
            this.alertInit(option.id); //课程详情
        }
        console.log(option.classId);
        if (option.classId) {
            this.commonResRoom(option.classId); //排课-根据课程ID获取常用教室列表
            // Taro.navigateTo({url: '../select-map/selectMap?id='+option.id});
        }
    }

    //修改课程
    alertInit = (id) => {
        console.log(id);
        const params = {
            url: '/wx/coach/resClassRefDetail',
            data:{
                id,
            }
        };
        request(params).then(res => {
            if (res) {
                if(res.code == 200) {
                    if (res && res.data) {
                        this.setState({
                            items: res.data
                        })
                    }
                }
            }
        })
    };

    //排课-根据课程ID获取常用教室列表
    //修改课程
    commonResRoom = (id) => {
        console.log(id + 'id');
        const params = {
            url: '/wx/coach/listCommonResRoom?classId='+id,
            data:{}
        };
        request(params).then(res => {
            if (res) {
                if(res.code == 200) {
                    if (res && res.data) {
                        this.setState({
                            roomWrap: res.data
                        })
                    }
                }
            }
        })
    };

    render() {
        const {roomWrap, items} = this.state;
        let option = this.$router.params;
        return (
            <View className="myPage">
                <View className="arranging-wrap">
                    {items.length != 0 ? (
                        <View className="arranging-kc-wrap" onClick={() => {Taro.redirectTo({url: '../select-coursey/selectCourse'})}}>
                            <View className="course-list flex-top">
                                <Image className="course-img" src={items.customAvatars[0]}/>
                                <View className="course-content flex-lr">
                                    <View>
                                        <View className="course-name text-overflow">{items.className}</View>
                                        <View className="flex-l">
                                            <View className="course-g flex-center">{items.classScaleName}</View>
                                            <View>¥{items.price}/节</View>
                                        </View>
                                        <View className="flex-l">
                                            {items.labelNames.map((value, is) => (
                                                <View className="course-gg flex-center" key={is}>{value}</View>
                                            ))}
                                        </View>
                                    </View>
                                    {/*<View className="course-btn flex-center">查看</View>*/}
                                </View>
                            </View>
                        </View>
                    ) : (
                        <View className="arranging-kc-wrap" onClick={() => {Taro.redirectTo({url: '../select-coursey/selectCourse'})}}>
                            <View className="name">1添加一门课程</View>
                            <View className="add-ke flex-center">+</View>
                        </View>
                    )}
                    {roomWrap.length != 0 ? (
                        <View className="arranging-kc-wrap">
                            <View className="flex-l classRoom-wrap">
                                <View className="heng"></View>
                                <View>选中的教室</View>
                            </View>
                            <View className="classRoom-content-wrap">
                                {roomWrap.map((item, index) => (
                                    <View className="class-top flex-lr" key={index} onClick={() => { Taro.navigateTo({url: '../arranging-coursey-next/arrangingCourseyNext?id=' + this.$router.params.id+'&roomId='+item.roomId})}}>
                                        <View className="class-title flex-l">
                                            <Image className="head-img" src={item.avatar}/>
                                            <View className="content-title">
                                                <View className="name">{item.roomName}</View>
                                                <View className="text-overflow">{item.addressDetail}</View>
                                            </View>
                                        </View>
                                    </View>
                                ))}
                            </View>
                            <View className="add-ke flex-center" onClick={() => {Taro.navigateTo({url: '../select-map/selectMap?id='+option.id})}}>+</View>
                        </View>
                    ) : (
                        <View className="arranging-kc-wrap">
                            <View className="name">2选择一间教室</View>
                            <View className="add-ke flex-center" onClick={() => {Taro.navigateTo({url: '../select-map/selectMap?id='+option.id})}}>+</View>
                        </View>
                    )}
                </View>
            </View>
        )
    }
}

const mapStateToProps = state => {
    let {} = state;
    return {
    }
};

const mapDispatchToProps = {
};

export default connect(mapStateToProps, mapDispatchToProps)(MyCoupon);



